/*
 * Stack_Pointer_Tasks.c
 *
 *  Created on: Jan 20, 2014
 *      Author: adijoshi
 */
//INCLUDES
#include "includes.h"

/*
 * Function: get_Stack_Pointer
 *
 * Inputs:	(None)
 * Outputs: contents of SP
 */

unsigned short get_Stack_Pointer()
{
	return read_from_memory(SP,DATA);
}

/*
 * Function: set_Stack_Pointer
 *
 * Inputs:	New contents of SP
 * Outputs: (None)
 */

void set_Stack_Pointer(unsigned short data)
{
	write_in_memory(SP,data,DATA);
}

/*
 * Function: pop
 *
 * Inputs:	(None)
 * Outputs: pop contents of the stack
 */
unsigned short pop()
{
	unsigned short stack_ptr,tmp_stack_ptr;
	stack_ptr = read_from_memory(SP,DATA);

	tmp_stack_ptr = stack_ptr;
	//increment stack ptr by 2
	stack_ptr += 2;
	// update SP
	write_in_memory(SP,stack_ptr,DATA);
	set_memory_status(INVALID_MEM_LOCATION,tmp_stack_ptr);

	// return data from memory at the unincremented address
	// Behavior is same as post increment
	// Read followed by SP = SP + 2
	return read_from_memory(stack_ptr - 2,DATA);
}

/*
 * Function: push
 *
 * Inputs:	content to push on the stack
 * Outputs: (None)
 */
void push(unsigned short data)
{
	unsigned short stack_ptr;

	stack_ptr = read_from_memory(SP,DATA);

	//decrement stack ptr by 2
	stack_ptr -= 2;
	write_in_memory(stack_ptr,data,DATA);

	// update SP
	// Behavior is same as pre increment
	// SP = SP - 2 followed by write

	write_in_memory(SP,stack_ptr,DATA);

}
